import Mock from "mockjs";

const Random = Mock.Random;

// 配置拦截 ajax 的请求时的行为，支持自定义
Mock.setup({
  timeout: "200-600", // 指定被拦截的 ajax 请求的响应时间
});

// 通用响应结构
const successResponse = (data) => {
  return {
    code: 200,
    data,
    message: "success",
  };
};

// Mock 数据
const mockData = {
  profile: {
    avatar:
      "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png",
    name: "张三",
    title: "全栈工程师",
    description:
      "热爱编程，专注于Web开发，有5年开发经验。擅长Vue.js、Node.js和Python等技术栈。",
  },

  skills: [
    { name: "Vue.js", level: 90, color: "#41B883" },
    { name: "JavaScript", level: 85, color: "#F7DF1E" },
    { name: "Node.js", level: 80, color: "#339933" },
    { name: "Python", level: 75, color: "#3776AB" },
    { name: "Docker", level: 70, color: "#2496ED" },
    { name: "MySQL", level: 75, color: "#4479A1" },
  ],

  projects: [
    {
      name: "企业管理系统",
      time: "2023-01 至 2023-12",
      description:
        "使用Vue.js和Element UI开发的企业级管理系统，实现了人员管理、资源调度等功能。",
    },
    {
      name: "电商平台",
      time: "2022-03 至 2022-12",
      description:
        "基于Node.js的电商平台后端开发，使用Express框架，实现了商品管理、订单处理等功能。",
    },
    {
      name: "数据分析平台",
      time: "2021-06 至 2022-02",
      description:
        "使用Python开发的数据分析平台，集成了数据可视化、报表导出等功能。",
    },
  ],
};

// 定义接口
Mock.mock("/api/profile", "get", () => {
  return successResponse(mockData.profile);
});

Mock.mock("/api/skills", "get", () => {
  return successResponse(mockData.skills);
});

Mock.mock("/api/projects", "get", () => {
  return successResponse(mockData.projects);
});

// 生成文章列表数据
const generateArticles = () => {
  const articles = [];
  for (let i = 0; i < 10; i++) {
    const article = {
      id: Random.id(),
      title: Random.ctitle(5, 15),
      description: Random.cparagraph(1, 3),
      author: Random.cname(),
      date: Random.date("yyyy-MM-dd"),
      readCount: Random.integer(100, 10000),
      category: Random.pick(["技术", "生活", "随笔", "前端", "后端"]),
      tags: new Array(Random.integer(1, 4))
        .fill()
        .map(() => Random.cword(2, 4)),
      coverImage: Random.image(
        "300x200",
        Random.color(),
        Random.color(),
        "png",
        Random.cword(2, 4)
      ),
    };
    articles.push(article);
  }
  return articles;
};

// 生成用户信息数据
const generateUserProfile = () => {
  return {
    id: Random.id(),
    username: Random.cname(),
    avatar: Random.image(
      "100x100",
      Random.color(),
      Random.color(),
      "png",
      "头像"
    ),
    email: Random.email(),
    bio: Random.cparagraph(1),
    registerDate: Random.date("yyyy-MM-dd"),
    followers: Random.integer(0, 1000),
    following: Random.integer(0, 1000),
  };
};

// 设置接口拦截
Mock.mock("/api/articles", "get", {
  code: 200,
  data: generateArticles(),
  message: "获取文章列表成功",
});

Mock.mock("/api/user/profile", "get", {
  code: 200,
  data: generateUserProfile(),
  message: "获取用户信息成功",
});

// 导出 mock 数据用于测试
export default Mock;
